package com.alen.sun.service.Impl;

import com.alen.sun.bean.Package;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.PackageMapper;
import com.alen.sun.service.PackageService;
import com.alen.sun.util.PhoneCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PackageServiceImpl implements PackageService {

    @Autowired
    private PackageMapper packageMapper;

    @Override
    public Boolean addPackage(Package package1) {
        //判断必要参数不为空
        if(package1.getPackageName()!=null && !package1.getPackageName().isEmpty()
            && package1.getPackagePrice()!=null && package1.getPackagePrice() != 0.0
            && package1.getPackageType()!=null
            && package1.getPackageImage()!=null && !package1.getPackageImage().isEmpty()){
            //生成id
            PhoneCode phoneCode = new PhoneCode();
            while (true){
                String packageId = phoneCode.generateCode();
                //判断id是否重复
                Integer count = packageMapper.selectById(packageId);
                if(count == null || count == 0){
                    //没有重复的,可以添加
                    package1.setPackageId(packageId);
                    //退出循环
                    break;
                }
            }
            //执行添加记录操作
            Boolean flag = packageMapper.addPackage(package1);
            return flag;
        }
        return false;
    }

    /**
     * 修改
     * @param package1
     * @return
     */
    @Override
    public Boolean updatePackage(Package package1) {
        //判断必要参数不为空
        if(package1.getPackageName()!=null && !package1.getPackageName().isEmpty()
                && package1.getPackagePrice()!=null && package1.getPackagePrice() != 0.0
                && package1.getPackageType()!=null
                && package1.getPackageImage()!=null && !package1.getPackageImage().isEmpty()){
            Boolean flag = packageMapper.updatePackage(package1);
            return flag;
        }
        return false;
    }

    /**
     * 删除
     * @param package1
     * @return
     */
    @Override
    public Boolean deletePackage(Package package1) {
        Boolean flag = packageMapper.deletePackage(package1);
        return flag;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<Package> packageList = packageMapper.page(start, pageSize);
        //2.查询总数
        Long count = packageMapper.count();
        PageBean pageBean = new PageBean(count,packageList);
        return pageBean;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = packageMapper.deleteByIds(ids);
        return flag;
    }
}
